﻿using LiveCharts.Wpf;
using System;
using System.Drawing;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using TempCtr.Model;

namespace TempCtr.MyControl
{
    public partial class TempCard : UserControl
    {
        public Thermostat Th { get; set; }

        public TempCard(Thermostat thermostat)
        {
            InitializeComponent();
            #region angularGauge1
            angularGauge1.Value = 0;
            angularGauge1.FromValue = -20;
            angularGauge1.ToValue = 40;
            angularGauge1.Wedge = 180;
            angularGauge1.NeedleFill = System.Windows.Media.Brushes.White;
            angularGauge1.TicksForeground = System.Windows.Media.Brushes.White;
            angularGauge1.Base.Foreground = System.Windows.Media.Brushes.White;
            angularGauge1.Base.FontWeight = FontWeights.Bold;
            angularGauge1.Base.FontSize = 14;
            angularGauge1.SectionsInnerRadius = 0.5;
            angularGauge1.LabelsStep = 10;
            angularGauge1.TickStep = 5;

            angularGauge1.Sections.Add(new AngularSection
            {
                FromValue = -20,
                ToValue = 0,
                Fill = System.Windows.Media.Brushes.LimeGreen
                
            });

            angularGauge1.Sections.Add(new AngularSection
            {
                FromValue = 0,
                ToValue = 10,
                Fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(247, 166, 37))
                
            });

            angularGauge1.Sections.Add(new AngularSection
            {
                FromValue = 10,
                ToValue = 40,
                Fill = new SolidColorBrush(System.Windows.Media.Color.FromRgb(254, 57, 57))
            });
            #endregion
            Th = thermostat;
            UpShowFirst();
            UpShowOnline(false);
        }

        public void UpShowOnline(bool isOnline)
        {
            Action DoAction = delegate ()
            {
                if (isOnline == true)//正常
                {
                    panel1.BackColor = System.Drawing.Color.FromArgb(0x66,0xcc,0xff);
                }
                else
                {
                    panel1.BackColor = System.Drawing.Color.Gray;
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowImfor(string num,string name,string age,string time,string pic)
        {
            Action DoAction = delegate ()
            {
                defNum.Text = num;
                defName.Text = name;
                defAge.Text = age;
                defEntryTime.Text = time;
                if (num == "") pictureBox1.Image = global::TempCtr.Properties.Resources._null;
                else if (pic == "") pictureBox1.Image = global::TempCtr.Properties.Resources.wu;
                else if (pic == "1") pictureBox1.Image = global::TempCtr.Properties.Resources.ica_jpg;
                else 
                { 
                    
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowFirst()
        {
            Action DoAction = delegate ()
            {
                LB_ID.Text = "柜号:" + Th.Num;
                LB_Temp.Text = "XXX℃";
                if (Th.Temp >= 40) angularGauge1.Value = 40;
                else angularGauge1.Value = Th.Temp;              
                
                defNum.Text = MySqlHelp.GetDefNum(Th.ID);

                if (defNum.Text != "")//判断编号是否为空
                {
                    //不为空则继续获取
                    defName.Text = MySqlHelp.GetDefName(Th.ID);
                    defAge.Text = MySqlHelp.GetDefAge(Th.ID);
                    defEntryTime.Text = MySqlHelp.GetDefTime(Th.ID);
                    string defPic = MySqlHelp.GetDefPic(Th.ID);
                    if (defPic == "") pictureBox1.Image = global::TempCtr.Properties.Resources.wu;
                    else
                    {
                        if(defPic == "1")
                        pictureBox1.Image = global::TempCtr.Properties.Resources.ica_jpg;
                        //将string转为图片文件，再显示
                    }
                }
                else
                {
                    //为空则其余均为空
                    defName.Text = "";
                    defAge.Text = "";
                    defEntryTime.Text = "";
                    pictureBox1.Image= global::TempCtr.Properties.Resources._null;
                }
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowTemp(byte type)
        {
            Action DoAction = delegate ()
            {
                LB_Temp.BackColor=LB_Temp.ForeColor;
                LB_Temp.ForeColor = panel1.BackColor;
                LB_Temp.Refresh();

                Thread.Sleep(50);
                LB_Temp.BackColor = System.Drawing.Color.Transparent;
                LB_ID.Text = "柜号:" + Th.Num;
                angularGauge1.Value = Th.Temp;
                if (System.Math.Abs(Th.Temp) >= 10)
                {
                    LB_Temp.Text = Th.Temp.ToString("00.0") + "℃";
                }
                if (System.Math.Abs(Th.Temp) < 10)
                {
                    LB_Temp.Text = Th.Temp.ToString("0.00") + "℃";
                }
                if (Th.Temp < 0)
                {
                    LB_Temp.Location = new System.Drawing.Point(72, 200);
                }
                else
                {
                    LB_Temp.Location = new System.Drawing.Point(78, 200);
                }

                if (type==0)//正常
                {
                    LB_Temp.ForeColor = System.Drawing.Color.Green;                
                }
                if(type == 1)//高温
                {
                    LB_Temp.ForeColor = System.Drawing.Color.Red;
                }
                if (type == 2)//低温
                {
                    LB_Temp.ForeColor = System.Drawing.Color.White;
                }                
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }


        private void Panel1_Click(object sender, EventArgs e)
        {          
            //temp_variation f1 = new temp_variation(Th);
            //f1.ShowDialog();
        }
    }
}
